package com.kzw.oa.dao.info.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.kzw.core.Constants;
import com.kzw.core.dao.impl.BaseDaoImpl;
import com.kzw.core.util.DateUtil;
import com.kzw.core.web.paging.PagingBean;
import com.kzw.oa.dao.info.NewsDao;
import com.kzw.oa.model.info.News;

public class NewsDaoImpl extends BaseDaoImpl<News> implements NewsDao{
	
	public NewsDaoImpl() {
		super(News.class);
	}

	@Override
	public List<News> findByTypeId(final Long typeId,final PagingBean pb) {
		final String hql = "from News n where n.newsType.typeId=?";
		Object[] params ={typeId};
		return findByHql(hql, params, pb);
	}

	@Override
	public List<News> findBySearch(Integer isNotice, String searchContent,PagingBean pb) {
		ArrayList params=new ArrayList();
		StringBuffer hql = new StringBuffer("from News n where n.isNotice= ? and n.status = ?");
		params.add(isNotice);
		params.add(Constants.FLAG_ACTIVATION);
		if(StringUtils.isNotEmpty(searchContent)){
			hql.append(" and (n.subject like ? or n.content like ?)");
			params.add("%"+searchContent+"%");
			params.add("%"+searchContent+"%");
		}
		hql.append(" and (n.expTime>? or n.expTime = null)");
		params.add(DateUtil.strToDate());
		hql.append(" order by n.updateTime desc");
		return findByHql(hql.toString(),params.toArray(), pb);
	}

	@Override
	public List<News> findImageNews(Long sectionId,PagingBean pb) {
		String hql="from News vo where vo.section.sectionId = ? " +
				"and (vo.expTime>? or vo.expTime = null) order by vo.updateTime desc";
		return findByHql(hql,new Object[]{sectionId,DateUtil.strToDate()},pb);
	}

	@Override
	public List<News> findImageCommonNews(Long sectionId,PagingBean pb) {
		String hql="from News vo where vo.section.sectionId = ? " +
				"and (vo.expTime>? or vo.expTime = null) " +
				"and vo.isNotice = 0 order by vo.updateTime desc";
		return findByHql(hql,new Object[]{sectionId,DateUtil.strToDate()},pb);
	}
	
}
